using System.Collections;
using UnityEngine;

public class UITools
{
	public static void SetActive(MonoBehaviour beh, bool active)
	{
		if (beh != null)
		{
			SetActive(beh.gameObject, active);
		}
	}

	public static void SetActive(Transform trans, bool active)
	{
		if (trans != null)
		{
			SetActive(trans.gameObject, active);
		}
	}

	public static void SetActive(UIWidget widget, bool active)
	{
		if (widget != null)
		{
			SetActive(widget.cachedGameObject, active);
		}
	}

	public static void SetActive(GameObject obj, bool active)
	{
		if (obj != null)
		{
			obj.SetActive(active);
		}
	}

	public static void ChangeStyle(UIStyledItem style, string styleName)
	{
		if (!(style == null))
		{
			style.SetStyle(styleName);
		}
	}

	public static void ChangeStyle(UIWidget widget, string styleName)
	{
		if (!(widget == null))
		{
			UIStyledItem component = widget.GetComponent<UIStyledItem>();
			ChangeStyle(component, styleName);
		}
	}

	public static void ChangeSprite(UISprite sprite, string spriteName)
	{
		if (sprite != null && sprite.spriteName != spriteName)
		{
			sprite.spriteName = spriteName;
		}
	}

	public static void Line(UISprite sprite, Vector3 start, Vector3 end)
	{
		float num = Vector3.Distance(start, end);
		Vector3[] worldCorners = sprite.worldCorners;
		float num2 = Mathf.Abs(worldCorners[0].y - worldCorners[1].y);
		float f = num / num2 * (float)sprite.height;
		sprite.height = Mathf.RoundToInt(f);
		sprite.cachedTransform.position = start;
		Vector3 normalized = (end - start).normalized;
		float num3 = Vector3.Angle(Vector3.up, normalized);
		if (start.x < end.x)
		{
			sprite.cachedTransform.rotation = Quaternion.Euler(Vector3.back * num3);
		}
		else
		{
			sprite.cachedTransform.rotation = Quaternion.Euler(Vector3.forward * num3);
		}
	}

	public static void LineLocalSpace(UISprite sprite, Vector3 start, Vector3 end)
	{
		float num = Vector3.Distance(start, end);
		float f = num;
		sprite.height = Mathf.RoundToInt(f);
		sprite.cachedTransform.localPosition = start;
		Vector3 normalized = (end - start).normalized;
		float num2 = Vector3.Angle(Vector3.up, normalized);
		if (start.x < end.x)
		{
			sprite.cachedTransform.localRotation = Quaternion.Euler(Vector3.back * num2);
		}
		else
		{
			sprite.cachedTransform.localRotation = Quaternion.Euler(Vector3.forward * num2);
		}
	}

	public static void SetScreenPosition(UIWidget widget, Vector3 screenPos)
	{
		if (!(widget == null))
		{
			Camera mainCamera = UICamera.mainCamera;
			if (!(mainCamera == null))
			{
				widget.cachedTransform.position = mainCamera.ScreenToWorldPoint(screenPos);
			}
		}
	}

	public static void ChangeText(UILabel label, int text)
	{
		ChangeText(label, text.ToString());
	}

	public static void ChangeText(UILabel label, string text)
	{
		if (text == null)
		{
			text = string.Empty;
		}
		if (label != null && label.text != text)
		{
			label.text = text;
		}
	}

	public static void SetColor(UIWidget widget, Color color)
	{
		if (!(widget == null))
		{
			widget.color = color;
		}
	}

	public static IEnumerator DoCenterOnScroll(UIWidget target, UIScrollView mScrollView, int repeats = 5)
	{
		return DoCenterOnScroll(target.cachedTransform, mScrollView, repeats);
	}

	public static IEnumerator DoCenterOnScroll(Transform target, UIScrollView mScrollView, int repeats = 5)
	{
		int repeat = 0;
		while (repeat < repeats)
		{
			CenterOnScroll(target, mScrollView);
			repeat++;
			yield return null;
		}
	}

	public static IEnumerator DoReposition(UITable table, int repeats = 2)
	{
		int repeat = 0;
		table.Reposition();
		while (repeat < repeats)
		{
			table.Reposition();
			repeat++;
			yield return null;
		}
	}

	public static void CenterOnScroll(Transform target, UIScrollView mScrollView)
	{
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		if (!(mScrollView == null) && !(target == null) && !(component == null))
		{
			Vector3[] worldCorners = component.worldCorners;
			Vector3 position = (worldCorners[2] + worldCorners[0]) * 0.5f;
			Transform cachedTransform = component.cachedTransform;
			Vector3 vector = cachedTransform.InverseTransformPoint(target.position);
			Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
			Vector3 vector3 = vector - vector2;
			if (!mScrollView.canMoveHorizontally)
			{
				vector3.x = 0f;
			}
			if (!mScrollView.canMoveVertically)
			{
				vector3.y = 0f;
			}
			vector3.z = 0f;
			float strength = 8f;
			SpringPanel.Begin(component.cachedGameObject, cachedTransform.localPosition - vector3, strength);
		}
	}

	public static void CenterWidgetOnScroll(UIWidget target, UIScrollView mScrollView)
	{
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		Debug.Log("CENTER WIDGET ON SCROLL " + target == null);
		if (!(mScrollView == null) && !(target == null) && !(component == null))
		{
			Vector3[] worldCorners = component.worldCorners;
			Vector3 position = (worldCorners[2] + worldCorners[0]) * 0.5f;
			Transform cachedTransform = component.cachedTransform;
			Vector3[] worldCorners2 = target.worldCorners;
			Vector3 position2 = (worldCorners2[2] + worldCorners2[0]) * 0.5f;
			Vector3 vector = cachedTransform.InverseTransformPoint(position2);
			Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
			Vector3 vector3 = vector - vector2;
			Debug.Log("Local Offset " + vector3);
			Debug.Log(string.Concat("centerPanel ", vector2, " Center target ", vector));
			if (!mScrollView.canMoveHorizontally)
			{
				vector3.x = 0f;
			}
			if (!mScrollView.canMoveVertically)
			{
				vector3.y = 0f;
			}
			vector3.z = 0f;
			float strength = 8f;
			SpringPanel.Begin(component.cachedGameObject, cachedTransform.localPosition - vector3, strength);
		}
	}

	public static void AlignToLeftOnScroll(UIWidget target, UIScrollView mScrollView, float padding, bool alignImediate = false)
	{
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		if (!(mScrollView == null) && !(target == null) && !(component == null))
		{
			Vector3[] worldCorners = component.worldCorners;
			Vector3 position = worldCorners[0];
			Transform cachedTransform = component.cachedTransform;
			Vector3 position2 = target.worldCorners[0];
			Vector3 vector = cachedTransform.InverseTransformPoint(position2);
			Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
			Vector3 vector3 = vector - vector2 - new Vector3(padding, padding, 0f);
			if (!mScrollView.canMoveHorizontally)
			{
				vector3.x = 0f;
			}
			if (!mScrollView.canMoveVertically)
			{
				vector3.y = 0f;
			}
			vector3.z = 0f;
			float strength = 8f;
			if (alignImediate)
			{
				component.transform.localPosition = cachedTransform.localPosition - vector3;
			}
			else
			{
				SpringPanel.Begin(component.cachedGameObject, cachedTransform.localPosition - vector3, strength);
			}
		}
	}

	public static void AlignToLeftOnScrollVisible(UIWidget target, UIScrollView mScrollView, UITable table, float padding, bool alignImediate = false)
	{
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		if (!(mScrollView == null) && !(target == null) && !(component == null))
		{
			Vector3[] worldCorners = component.worldCorners;
			Vector3 position = worldCorners[0];
			Vector3 position2 = worldCorners[2];
			Transform cachedTransform = component.cachedTransform;
			Vector3 position3 = target.worldCorners[0];
			Bounds bounds = NGUIMath.CalculateAbsoluteWidgetBounds(table.transform);
			Vector3 vector = cachedTransform.InverseTransformPoint(position3);
			Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
			Vector3 vector3 = vector - vector2 - new Vector3(padding, padding, 0f);
			Vector3 vector4 = cachedTransform.InverseTransformPoint(bounds.max);
			Vector3 vector5 = cachedTransform.InverseTransformPoint(position2);
			Vector3 vector6 = vector4 - vector5 - new Vector3(padding, padding, 0f);
			if (!mScrollView.canMoveHorizontally)
			{
				vector3.x = 0f;
				vector6.x = 0f;
			}
			if (!mScrollView.canMoveVertically)
			{
				vector3.y = 0f;
				vector6.y = 0f;
			}
			vector3.z = 0f;
			vector6.z = 0f;
			float strength = 8f;
			Vector3 vector7 = cachedTransform.localPosition - vector3;
			Vector3 vector8 = cachedTransform.localPosition - vector6;
			if (vector8.x > vector7.x)
			{
				vector7 = vector8;
			}
			Debug.Log(vector7);
			Debug.Log(vector8);
			if (alignImediate)
			{
				component.transform.localPosition = vector7;
			}
			else
			{
				SpringPanel.Begin(component.cachedGameObject, vector7, strength);
			}
		}
	}

	public static void AlignToRightOnScroll(UIWidget target, UIScrollView mScrollView, float padding)
	{
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		if (!(mScrollView == null) && !(target == null) && !(component == null))
		{
			Vector3[] worldCorners = component.worldCorners;
			Vector3 position = worldCorners[2];
			Transform cachedTransform = component.cachedTransform;
			Vector3 position2 = target.worldCorners[2];
			Vector3 vector = cachedTransform.InverseTransformPoint(position2);
			Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
			Vector3 vector3 = vector - vector2 - new Vector3(padding, padding, 0f);
			if (!mScrollView.canMoveHorizontally)
			{
				vector3.x = 0f;
			}
			if (!mScrollView.canMoveVertically)
			{
				vector3.y = 0f;
			}
			vector3.z = 0f;
			float strength = 8f;
			SpringPanel.Begin(component.cachedGameObject, cachedTransform.localPosition - vector3, strength);
		}
	}

	public static IEnumerator DoAlignToTopOnScroll(UIWidget target, UIScrollView mScrollView, float padding, int repeats = 4)
	{
		for (int repeat = 0; repeat < repeats; repeat++)
		{
			AlignToTopOnScroll(target, mScrollView, padding);
			yield return null;
		}
	}

	public static IEnumerator DoAlignToLeftOnScroll(UIWidget target, UIScrollView mScrollView, float padding, int repeats = 4)
	{
		for (int repeat = 0; repeat < repeats; repeat++)
		{
			AlignToLeftOnScroll(target, mScrollView, padding);
			yield return null;
		}
	}

	public static IEnumerator DoAlignToRightOnScroll(UIWidget target, UIScrollView mScrollView, float padding, int repeats = 4)
	{
		for (int repeat = 0; repeat < repeats; repeat++)
		{
			AlignToRightOnScroll(target, mScrollView, padding);
			yield return null;
		}
	}

	public static IEnumerator DoAlignToLeftOnScrollVisible(UIWidget target, UIScrollView mScrollView, UITable table, float padding, int repeats = 4)
	{
		for (int repeat = 0; repeat < repeats; repeat++)
		{
			AlignToLeftOnScrollVisible(target, mScrollView, table, padding);
			yield return null;
		}
	}

	public static void AlignToTopOnScroll(UIWidget target, UIScrollView mScrollView, float padding)
	{
		if (mScrollView == null)
		{
			return;
		}
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		if (component == null)
		{
			return;
		}
		if (mScrollView == null || target == null || component == null)
		{
			Debug.Log("null exit");
			return;
		}
		Vector3[] worldCorners = component.worldCorners;
		Vector3 position = worldCorners[1];
		Transform cachedTransform = component.cachedTransform;
		Vector3 position2 = target.worldCorners[1];
		Vector3 vector = cachedTransform.InverseTransformPoint(position2);
		Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
		Vector3 vector3 = vector - vector2 - new Vector3(0f, padding, 0f);
		if (!mScrollView.canMoveHorizontally)
		{
			vector3.x = 0f;
		}
		if (!mScrollView.canMoveVertically)
		{
			vector3.y = 0f;
		}
		vector3.z = 0f;
		float strength = 8f;
		SpringPanel.Begin(component.cachedGameObject, cachedTransform.localPosition - vector3, strength);
	}

	public static void AlignToBottomOnScroll(UIWidget target, UIScrollView mScrollView, float padding)
	{
		UIPanel component = mScrollView.GetComponent<UIPanel>();
		if (!(mScrollView == null) && !(target == null) && !(component == null))
		{
			Vector3[] worldCorners = component.worldCorners;
			Vector3 position = worldCorners[0];
			Transform cachedTransform = component.cachedTransform;
			Vector3 position2 = target.worldCorners[0];
			Vector3 vector = cachedTransform.InverseTransformPoint(position2);
			Vector3 vector2 = cachedTransform.InverseTransformPoint(position);
			Vector3 vector3 = vector - vector2 - new Vector3(0f, padding, 0f);
			if (!mScrollView.canMoveHorizontally)
			{
				vector3.x = 0f;
			}
			if (!mScrollView.canMoveVertically)
			{
				vector3.y = 0f;
			}
			vector3.z = 0f;
			float strength = 8f;
			SpringPanel.Begin(component.cachedGameObject, cachedTransform.localPosition - vector3, strength);
		}
	}
}
